<template>
	<view class="entire">
		<cu-custom bgColor="bg-Midnight-Blue">
			<block style="color: #fff;" slot="backText" @click="cancellation">取消</block>
			<block slot="content" style="color: #fff;">修改帖子</block>
		</cu-custom>
		<!-- <view style="padding: 80rpx 0 0 30rpx;">
			<input type="text" v-model="title" placeholder="文章标题" style="color: #fff;" />
		</view> -->
		<view class="cu-form-group margin-top" style="background-color: #112858;color: #fff;">
			<textarea maxlength="-1" v-model="content" :disabled="modalName!=null" @input="textareaAInput"
				placeholder="分享新鲜事..." style="height: 800rpx;"></textarea>
		</view>

		<view class="cu-bar bg-white margin-top">
			<view class="action">
				文件上传
			</view>
			<view class="action">
			</view>
		</view>
		<view class="cu-form-group">
			<view class="grid col-4 grid-square flex-sub">
				<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage"
					:data-url="imgList[index]">
					<template v-if="isImage(imgList[index])">
						<image :src="imgList[index]" mode="aspectFill"></image>
					</template>
					<template v-else-if="isVideo(imgList[index])">
						<video :src="imgList[index]" controls></video>
					</template>
					<template v-else>
						<view style="display: flex;align-items: center;justify-content: center;">文件</view>
					</template>
					<!-- <image :src="imgList[index]" mode="aspectFill"></image> -->
					<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
						<text class='cuIcon-close'></text>
					</view>
				</view>
				<view class="solids" @tap="ChooseImage" v-if="imgList.length<9">
					<text class='cuIcon-cameraadd'></text>
				</view>
			</view>
		</view>

		<view class="sending" @click="sending">
			修改
		</view>
		<view class="cancellation" @click="cancellation">
			取消
		</view>
		<view style="width: 100%;height: 180rpx;"></view>
	</view>
</template>

<script>
	import forum from '@/api/forum.js';
	import {
		img_upload
	} from '@/common/config'
	export default {
		data() {
			return {
				token: '',
				title: '',
				content: '',
				imgList: [],
				img_file: '',
				video_file: '',
				file: '',
				files: [],
				receivedData: [],
				updatePost: [],
			}
		},
		onLoad() {
			const token = uni.getStorageSync('token');
			this.token = token
			const updateList = uni.getStorageSync('arrayList');
			this.updatePost = uni.getStorageSync('arrayList');
			console.log(this.updatePost)
			// this.content = this.removeHtmlTags(updateList.post_content)
			this.content = updateList.post_content
			console.log(this.content)
			let imgLists = [...updateList.file_img, ...updateList.file_video, ...updateList.file_data];
			this.imgList = imgLists
			this.files = imgLists
		},
		methods: {
			ChooseImage() {
				uni.chooseImage({
					count: 9, //默认9 
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							this.uploadFile(res.tempFilePaths[i]);
						}
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
					}
				});
			},
			isImageFile(extension) {
				const imageExtensions = ['jpg', 'png', 'bmp', 'jpeg', 'gif', 'webp'];
				return imageExtensions.includes(extension.toLowerCase());
			},
			isVideoFile(extension) {
				const videoExtensions = ['mp4', 'webm'];
				return videoExtensions.includes(extension.toLowerCase());
			},
			isImage(path) {
			      return /\.(jpg|jpeg|png|gif)$/.test(path.filename.toLowerCase());
			},
			isVideo(path) {
			      return /\.(mp4|mov|avi|mkv)$/.test(path.filename.toLowerCase());
			},
			uploadFile(filePath) {
				uni.uploadFile({
					url: img_upload,
					filePath: filePath,
					name: 'file',
					formData: {
						'user': 'test',
						'token': this.token
					},
					success: (uploadRes) => {
						const responseObject = JSON.parse(uploadRes.data);
						if (responseObject.code == 403) {
							uni.showToast({
								title: responseObject.msg,
								icon: 'error'
							})
						} else {
							this.files.push(responseObject.data.url);
						}
					},
					fail: (error) => {
						console.log(error);
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			removeHtmlTags(str) {
				return str.replace(/<[^>]*>/g, '');
			},
			DelImg(e) {
				uni.showModal({
					title: '',
					content: '确定要删除这段回忆吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							console.log('Before Deletion:', this.files, this.imgList);
							// this.files.splice(e.currentTarget.dataset.index, 1)
							this.imgList.splice(e.currentTarget.dataset.index, 1)
							console.log('After Deletion:', this.files, this.imgList);
						}
					}
				})
			},
			textareaAInput(e) {
				console.log(e)
			},
			cancellation() {
				uni.redirectTo({
					url: '/pages/index/index',
				});
			},
			sending() {
				for (var i = 0; i < this.files.length; i++) {
					const fileExtension = this.files[i].split('.').pop();
					if (this.isImageFile(fileExtension)) {
						this.img_file += this.files[i] + ','
					} else if (this.isVideoFile(fileExtension)) {
						this.video_file += this.files[i] + ','
					} else {
						this.file += this.files[i] + ','
					}
				}
				forum.getWebConfig().then(res => {
					console.log(res)
					if (res.code == 1) {
						forum.updatePost({
							token: this.token,
							id: this.updatePost.id,
							title: '',
							content: this.content,
							category_id: res.data.web_qa_community_channels_id,
							category_type: this.updatePost.category_type,
							file: this.file,
							fileImgs: this.img_file,
							fileVideos: this.video_file,
						}).then(res => {
							if (res.code == 1) {
								uni.showToast({
									title: '修改成功',
									icon: 'success'
								})
								this.cancellation()
							}
						})
					}
				})
			}
		}
	}
</script>

<style>
	.entire {
		width: 100%;
		background-color: #112858;
	}

	.cu-bar {
		border-bottom: 0;
		z-index: 9990 !important;
	}

	.sending {
		width: 120rpx;
		height: 60rpx;
		background-color: #275EAD;
		border-radius: 16rpx;
		color: #fff;
		line-height: 60rpx;
		text-align: center;
		position: fixed;
		top: 160rpx;
		right: 40rpx;
	}

	.cancellation {
		width: 120rpx;
		height: 60rpx;
		color: #fff;
		line-height: 60rpx;
		text-align: center;
		position: fixed;
		top: 105rpx;
		left: 40rpx;
		z-index: 9999 !important;
	}
</style>